package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.common.websocket.model.SocketMessage;
import com.jcfk.system.api.utils.DictionaryUtils;

import com.jcfk.system.domain.dto.SendMsgDTO;
import com.jcfk.system.domain.dto.SysNoticeDTO;
import com.jcfk.system.domain.po.SysInternalMessage;
import com.jcfk.system.service.SysDictionaryItemService;
import com.jcfk.system.service.WebSocketMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jcfk.system.domain.query.SysMessageQuery;
import com.jcfk.system.domain.dto.SysInternalMessageDTO;
import com.jcfk.system.service.SysInternalMessageService;

import java.util.List;


/**
 * 系统内部消息 服务接口
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2023-11-27 09:38:04
 */
@Api(tags = {"系统内部消息"})
@RestController
@RequestMapping("sysmessage")
public class SysInternalMessageController {

    @Autowired
    private SysInternalMessageService sysInternalMessageService;

    /**
     * 发送站内消息
     */
    @Autowired
    private WebSocketMessageService service;


    @Autowired
    private SysDictionaryItemService sysDictionaryItemService;
    @Autowired
    private DictionaryUtils dictionaryUtils;

    /**
     * 分页列表
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysMessageQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询")
    @PostMapping("/queryByPage")
    @ResponseBody
    public RetPage<SysInternalMessageDTO> queryByPage(@RequestBody ReqPageParam<SysMessageQuery> requestParam) {
        DataPage<SysInternalMessageDTO> page = new DataPage<SysInternalMessageDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), new OrderItem("send_time", false), SysInternalMessageDTO.class);
        // DataPage<SysInternalMessageDTO> page = new DataPage<SysInternalMessageDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), SysInternalMessageDTO.class);
        page = sysInternalMessageService.queryByPage(page, requestParam.getData());

        dictionaryUtils.bindDictionaryLabel(page.getRecords(), "MsgType", "msgType", "msgTypeLable");

        return new RetPage<>(page);
    }


    /**
     * 根据id 查询详情
     */
    @ApiOperation("查询")
    @GetMapping("/get/{id}")
    @ResponseBody
    public RetResult<SysInternalMessageDTO> info(@PathVariable("id") String id) {
        SysInternalMessageDTO item = sysInternalMessageService.get(id);
        dictionaryUtils.bindDictionaryLabel(item, "MsgType", "msgType", "msgTypeLable");

        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    @ResponseBody
    @RequestLogHeader(modular = "管理", businessType = "保存")
    public RetResult<?> save(@RequestBody SysInternalMessageDTO sysMessage) {
        // 数据校验
        RetResult ret = sysInternalMessageService.isValid(sysMessage);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(sysMessage.getId())) {
            // 新增
            return RetResult.ok(sysInternalMessageService.insert(sysMessage));
        } else {
            // 更新
            return RetResult.ok(sysInternalMessageService.update(sysMessage));
        }

    }

    /**
     * 批量删除
     */
    @ApiOperation("批量删除")
    @ResponseBody
    @PostMapping("/delete")
    @RequestLogHeader(modular = "管理", businessType = "批量删除")
    public RetResult<?> deleteList(@RequestBody SysInternalMessageDTO sysMessage) {
        return RetResult.ok(sysInternalMessageService.logicDeleteList(sysMessage));
    }


    /**
     * 消息管理  发送消息
     *
     * @param dto 发送消息dto
     * @return
     */
    @RequestLogHeader(modular = "消息管理", businessType = "发送")
    @PostMapping("sendMessage")
    public RetResult<?> sendMessage(@RequestBody SendMsgDTO dto) {
        try {
            List<SysInternalMessage> list = sysInternalMessageService.SendMessage(dto);
            for (SysInternalMessage internalMessage : list) {
                SocketMessage message = new SocketMessage();
                message.setMessageType("internalMessage");
                message.setUrl(dto.getUrl());
                message.setMsgId(internalMessage.getId());
                message.setMessageContent(dto.getTitle());
                message.setMessageLevel(dto.getMessageLevel());
                service.sendMessage(internalMessage.getRecevieUserId(), message);
            }
        } catch (Exception e) {
            return RetResult.fail("消息发送失败");
        }
        return RetResult.ok();
    }


    /**
     * 根据接收者获取前n条未读消息列表
     *
     * @param query 获取条件
     * @return
     */
    @PostMapping("getUnRead")
    @RequestLogHeader(modular = "消息管理", businessType = "获取当前用户未读消息")
    public RetResult<List<SysInternalMessageDTO>> getUnReadByRecevier(@RequestBody SysMessageQuery query) {
        return RetResult.ok(sysInternalMessageService.getUnReadByRecevier(query));
    }

    /**
     * 消息管理  获取当前用户未读消息数量
     *
     * @return
     */
    @PostMapping("userUnreadMessCount")
    public RetResult<Integer> userUnreadMessCount() {
        return RetResult.ok(sysInternalMessageService.getcount());
    }

    @PostMapping("getReportAlarmMessage")
    public RetResult<Integer> getReportAlarmMessage() {
        return RetResult.ok(sysInternalMessageService.getReportAlarmMessage());
    }

    @PostMapping("userreadMessCount")
    public RetResult<Integer> userreadMessCount() {
        return RetResult.ok(sysInternalMessageService.getreadcount());
    }
}
